# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function

from ..ops import Ref, Mean, EMA
from ..base import OpsFeature

from .close import Close

class TRIX(OpsFeature):
    """Technical Indicator - TRIX

    - TR  = EMA(EMA(EMA(CLOSE, N), N), N)
    - TRIX = (TR - REF(TR, 1)) / REF(TR, 1) * 100
    """
    def __init__(self, feature=None, N=12):
        self.feature = feature or Close()
        self.N = N

    def __str__(self):
        name = type(self).__name__.lower()
        if str(self.feature) != 'close':
            name += '_'+str(self.feature)
        if self.N != 12:
            name += '_'+str(self.N)
        return name
    
    def _load_internal(self, instrument, start_time, end_time, freq):
        tr = EMA(EMA(EMA(self.feature, self.N), self.N), self.N)
        trix = (tr - Ref(tr, 1)) / Ref(tr, 1) * 100
        return trix.load(instrument, start_time, end_time, freq)


class TRMA(OpsFeature):
    """Technical Indicator - TRMA

    - TRMA = MA(TRIX, N)
    """
    def __init__(self, feature=None, N1=12, N2=20):
        self.feature = feature or Close()
        self.N1 = N1
        self.N2 = N2

    def __str__(self):
        name = type(self).__name__.lower()
        if str(self.feature) != 'close':
            name += '_'+str(self.feature)
        if self.N1 != 12 or self.N2 != 20:
            name += '_{}_{}'.format(self.N1, self.N2)
        return name
    
    def _load_internal(self, instrument, start_time, end_time, freq):
        trix = TRIX(self.feature, self.N1)
        trma = Mean(trix, self.N2)
        return trma.load(instrument, start_time, end_time, freq)
